# -*- coding: utf-8 -*-

import requests
from retrying import retry


# 获取源码内容
@retry(stop_max_attempt_number=3)
def get_html(url):
    print('重复请求')
    head = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    }
    res = requests.get(url, headers=head)
    # 断言  理解为if    res.status.code 获取响应的状态码
    assert res.status_code == 200  # 如果条件成立，代码正常执行， 如果条件不成立，直接报错
    return res.text  # 返回响应内容

def parse_html(url):
    try:
        html_str = get_html(url)  #  html_str = get_html(url) = res.text
    except Exception as e:
        print(e)
        html_str = None
    return html_str  # 百度源码信息  res.text


# 定义了一个url
url = 'https://www.baidu.com'
# parse_html(url) 调用函数
data = parse_html(url)  #  parse_html(url) == rex.text
print(data)